﻿using JUtil;
using System.Collections.Generic;
using System.Text;

namespace JCodeGen
{
    public class DataObjectGenerator : CodeGenBase
    {
        private readonly List<FieldSchema> lstFieldSchema;

        private readonly string DataObjectTypeName;

        private readonly CodeGenFileTypes fileType;

        private string properties
        {
            get
            {
                string result = string.Empty;

                foreach (FieldSchema fieldSchema in lstFieldSchema)
                {
                    result += property(fieldSchema);
                    result += "\n";
                }

                return result;
            }
        }

        private string property(FieldSchema fieldSchema)
        {
            string result = string.Empty;

            string name = fieldSchema.ColumnName;
            string type = fieldSchema.DataType.ToString();
            
            switch (fileType)
            {
                case CodeGenFileTypes.cs: 
                    {
                        //
                        // C# style
                        // public string CustomerID { get; set; }
                        //

                        //
                        // 大括號處理
                        // http://www.code-design.cn/article/20100728/2754.aspx
                        //
                        result = string.Format("public {0} {1} {{ get; set; }}", type, name);
                        break; 
                    }

                case CodeGenFileTypes.vb: 
                    {
                        /*
                        Public Property CustomerID() As String
                            Get
                                Return m_CustomerID
                            End Get
                            Set
                                m_CustomerID = Value
                            End Set
                        End Property
                        Private m_CustomerID As String
                        */

                        string sinppet = string.Empty;

                        StringBuilder sb = new StringBuilder();

                        // Property
                        sinppet = string.Format("Public Property {0}() As {1}", name, type);
                        sb.AppendLine(sinppet);

                        /// Get
                        sb.AppendLine("Get");
                        sinppet = string.Format("Return m_{0}", name);
                        sb.AppendLine(sinppet);
                        sb.AppendLine("End Get");
                        /// Set
                        sb.AppendLine("Set");
                        sinppet = string.Format("m_{0} = Value", name);
                        sb.AppendLine(sinppet);
                        sb.AppendLine("End Set");

                        sb.AppendLine("End Property");
                        sinppet = string.Format("Private m_{0} As {1}", name, type);
                        sb.AppendLine(sinppet);

                        result = sb.ToString();
                        break; 
                    }
            }
            
            return result;
        }

        public DataObjectGenerator(
            DatabaseMode databaseMode,
            string connectionString,
            string SelectSQL,
            string DataObjectTypeName,
            CodeGenFileTypes fileType)
        {
            lstFieldSchema = FieldSchema.GetTableSchema(
                                new Database(databaseMode, connectionString),
                                SelectSQL);

            this.DataObjectTypeName = DataObjectTypeName;

            this.fileType = fileType;
        }

        protected override void SetReplacement()
        {
            AddReplacement("DataObjectTypeName", DataObjectTypeName);
            AddReplacement("properties", properties);
        }


    } // end of DataObjectOperatorGenerator
}
